function Pager(obj){
	this.width = obj.width==undefined?"100%":obj.width;
	this.cm = obj.cm;
	this.id = obj.id;
	this.total = obj.total;
	this.root = obj.root;
	this.prikey = obj.prikey;
	this.renderTo = obj.renderTo;
	this.checkbox = obj.checkbox?true:false;//boolean
	this.radiobox = obj.radiobox?true:false;
	this.val = null;
	this.data = obj.data;
	this.form = obj.formid;
	this.url = obj.url;
	this.dbclick = obj.dbclick==undefined?true:obj.dbclick;
	this.emptyTxt = obj.emptyMsg==undefined?"未找到任何记录!":obj.emptyMsg;
	this.waittingMsg = obj.loadingMsg ==undefined? "加载中，请稍候...":obj.loadingMsg;
}
Pager.prototype.paginate = function(){
	if (this.renderTo!=null && this.renderTo!=undefined){
		var ob = this.cm.columns;
		 this.id = this.renderTo+"t";
		var tab = "<table class=dq_dt_tab_style cellpadding=0 cellspacing='0' id="+ this.id+">" +
		"<tr class=dq_dt_tab_th>"+
    	"<td class=dq_dt_tab_chetd>";
		var val_ = [];
		var td = null;
		var p = this;
		if (this.checkbox)
			{
			tab+="<input type=checkbox class=dq_dt_tab_chebox wencheck"+this.id+" onClick=cheAllRow(this) name='checkbox' id='dq_dt_tab_chebox_id'/>";
			}else if (this.radiobox){
				tab+="#";
			}
		tab+="</td>";
		for(var j=0;j<ob.length;j++){
			ocm = ob[j];
			if (ocm == undefined)continue;
			tab+="<td width="+ocm.width+"><div>";
			if (ocm.sort){
				if (this.sorttype != undefined && this.sortby!=undefined && this.sortby == ocm.value){ // 进行排序
						    if ("asc" == this.sortby){
						    	tab+="<a class='dq_dt_tab_px02 sort' sid="+ocm.value+" sort="+ocm.value+">"+ocm.name+"</a>";
						    }else{
						    	tab+="<a class='dq_dt_tab_px sort' sid="+ocm.value+" sort="+ocm.value+">"+ocm.name+"</a>";
						    }
							
				}else{
					tab+="<a class='sort' sid="+ocm.value+" sort="+ocm.value+">"+ocm.name+"</a>";
				}
			}
			else{
			tab+=""+ocm.name+"</div></td>";
			}
		}
		$("#"+this.renderTo).append(tab+"</tr></table>");
		$(".sort").click(function(){
			var sorttype = "desc";
			// px 降 px02升
			if ($(this).hasClass("dq_dt_tab_px")){
				//升序
				$(this).removeClass("dq_dt_tab_px");
				$(this).addClass("dq_dt_tab_px02");
				sorttype="asc";
			}else if ($(this).hasClass("dq_dt_tab_px02")){
				// 降
				$(this).removeClass("dq_dt_tab_px02");
				$(this).addClass("dq_dt_tab_px");
				sorttype="desc";
			}
			else{
				//降
				$(this).addClass("dq_dt_tab_px");
			}
			p.data.sorttype = sorttype;
			p.data.sortby = $(this).attr("sort");
			p.load();
		});
	}
	return;
};
function Column(obj){//{name:"姓名",value:"name"}
	this.columns = obj.columns;
	this.labelAlign = obj.labelAlign==undefined?"":obj.labelAlign;
	this.order = obj.order?true:false;
	this.html = obj.html;
}
function Reader(obj){
	this.url = obj.url;
	this.msg = obj.msg;
	this.start = obj.start==null?0:obj.start;
	this.end = obj.end == null?10:obj.end;
	this.emptyTxt = obj.emptyTxt==undefined?obj.emptyTxt:"没有任何记录!";
}
var index = 0;
var focus = false ;
Pager.prototype.load=function(){
	Wloading3(this.waittingMsg);
	var obj;
	obj = this;
	var fid = this.form;
	var url = this.url;
	//var params = null;
	var querys = null;
	if (fid != undefined){ // 序列查询表单
		$("#"+fid).find("input").each(function(i){
			if ($(this).val()==$(this).attr("txt")){
				obj.data[$(this).attr("name")] = "";
			}else{
				obj.data[$(this).attr("name")] = $(this).val();
			}
		});
		$("#"+fid).find("select").each(function(j){
			obj.data[$(this).attr("name")] = $(this).val();
		});
		 if (focus == true){ // 查询键按后搜索字加红
			 querys = [];
			 $("#"+fid).find("input").each(function(){
				 if ($(this).val() != ""){
					 querys.push($(this).val());
				 }
			 });
		 }
	}
	$.ajax({
		type:"post",
		url:url,
		data:this.data,
		success:function(da){
			if (da.sessionTimeout){
				parent.window.location.href = "login.action";
			}
			$("#dq_dt_tab_chebox_id").removeAttr("checked");
				 var root = da.root;
				 var tr = "";
				 var val = obj.cm.columns;
				 $("#"+obj.id+" .dq_dt_tab_tr").remove();
				 var render = obj.renderTo;
				 if (val == undefined || val == null){
					 $("#"+render).empty();
				 }
				 $("#"+render+"1").remove();
				 $("#"+render+"s").remove();
				 var oval = null;
				 var hei = 29;
				 var lenght = root.length;
				 var l2= val.length;
				 var old_val = "";
				 for(var i=0;i < lenght;i++){
					 var h = root[i];
					 tr = "<tr class='dq_dt_tab_tr' >";
					 if (obj.checkbox)
						 tr+= "<td class='dq_dt_tab_chetd' onclick='check_one(this)'><input type='checkbox' onclick='check_one(this)' class='dq_dt_tab_chebox'  name='checkbox_dq' value='"+h[obj.prikey]+"' id='"+render+""+i+"' /> </td>";
					 else if (obj.radiobox){
						 tr+= "<td class='dq_dt_tab_chetd ' onclick='check_one(this)'><input type='radio' onclick='check_one(this)' class='dq_dt_tab_chebox'  name='checkbox' value='"+h[obj.prikey]+"' id='"+render+""+i+"'/> </td>";
					 }
					 for(var j=0;j<l2;j++){
						 
						 oval = val[j];
						
						 if (h[oval.value] == "null" || h[oval.value] == undefined){
							 h[oval.value] = "";
						 }
						 h[oval.value] = h[oval.value].toString();
						 old_val = h[oval.value];
						 if (focus == true){
							 for(var m=0;m<querys.length;m++ ){
									h[oval.value] =  h[oval.value].replace(querys[m],"<font color='red'>"+querys[m]+"</font>");
							 }
						 }
						 hei = oval.height == undefined?hei:oval.height;
						 oval.align = oval.align == undefined?"center":oval.align;
						 if (oval.format!=undefined && oval.format != ""){
							 tr+="<td height="+hei+"  align='"+oval.align+"' onclick='check_one(this)'><span class='dq_dt_tab_norspan dq_dt_tab_noct' style='"+oval.style+"'>"+oval.format.replace("$val",h[oval.value]).replace("$val",h[oval.value])+"</span></td>";
							 continue;
						 }else if (oval.valFormat != undefined){
							 if ($.isFunction(oval.valFormat)){
								 tr+="<td  height="+hei+"  align='"+oval.align+"' onclick='check_one(this)'><span class='dq_dt_tab_norspan dq_dt_tab_noct' style='"+oval.style+"'>"+ oval.valFormat(h[oval.value])+"</span></td>";
								 continue;
								}
						 }
						 else if (oval.success != undefined){
							 if ($.isFunction(oval.success)){
								 tr+="<td  height="+hei+"  align='"+oval.align+"' onclick='check_one(this)'><span class='dq_dt_tab_norspan dq_dt_tab_noct' style='"+oval.style+"'>"+ oval.success(h)+"</span></td>";
								 continue;
								}
						 }else if (oval.rowno){
								 tr+="<td  height="+hei+"  align='"+oval.align+"' onclick='check_one(this)'><span class='dq_dt_tab_norspan dq_dt_tab_noct' style='"+oval.style+"'>"+ i+"</span></td>";
								 continue;
						 }
						 tr+="<td title='"+old_val+"' height="+hei+" onclick='check_one(this)' align='"+oval.align+"' ><span class='dq_dt_tab_norspan dq_dt_tab_noct' style='"+oval.style+"'>"+h[oval.value]+"</span></td>";
					 }
					 $("#"+obj.id).append(tr+"</tr>");
				 }
				 var start = obj.data.start;
				 var end = obj.data.end;
				 var pageSize = parseInt(end) - parseInt(start);
				 var total = da.total;
				 $("#"+render+"c").text("0");
				 $("#"+render+"pageDiv").remove();
				 if (parseInt(total) ==0 || root.length == 0){
					 closeDialog();
					 $("#"+render).append("<span id='"+render+"s' style='line-height:60px;height:60px;padding-left:50px;border:1px solid rgb(206, 229, 249);display:block;background:url(frame/images/info.gif) left #fff8d9 no-repeat '><font color='green' size='+1'>"+obj.emptyTxt+"!</font></span>");
					 return;
				 }else{
					 $("#"+render+"c").text(total);
					 // 分页
					 var page_ = "<div class='pageDiv' id='"+render+"pageDiv'><div class='fl' style='border:1px solid #cfcece; border-right:none; font-size:0;'><label class='chOne'>每页显示";
					 page_+="<select class='smSele' id='select'>";
					  var size = parseInt(pageSize);
					  var op = [10,20,40,60];
					  for (var m=0;m < op.length;m++){
						  if ( size == op[m]){
							  page_+="<option value='"+op[m]+"' selected>"+op[m]+"</option>";
							  continue;
						  }
						  page_+="<option value='"+op[m]+"'>"+op[m]+"</option>";
						  
					  }
					  
					  page_+="</select>条</label>";
					 page_+="<label class='chOne'>共"+total+"条数据</label><label class='chOne'><a class='refrY' id='dq_dt_refresh'>刷新列表</a></label></div>";
					 page_+="<div class='fr tcdPageCode' id='pagenow' style='border:1px solid #cfcece; border-right:none; font-size:12px;'></div>";
					 
					 $("#"+render).parent().append(page_+"</div>");
					 var pageCount = parseInt((total-1)/size+1); 
					 var current = parseInt(parseInt(end)/size);
					 $(".tcdPageCode").createPage({
					        pageCount:pageCount,
					        current:current,
					        backFn:function(p){
					            obj.data.end = parseInt(p)*size;
					            obj.data.start = (parseInt(p)-1)*size;
					            obj.load();
					        }
					    });
				 }
				 
				  $("#select").change(function(){
					  obj.data.start = 0; // 第一页开始
					  obj.data.end = (parseInt($(this).val())+parseInt(obj.data.start));
					  obj.load();
				  }
					  ) ;
				  $("#dq_dt_refresh").click(function(){
					  obj.load();
				  });
				  //
				 
				  if( index == 1){
					  $("#pager_search").click(function(){
						  focus = true;
						  obj.load();// 查询按钮
					  });
					  $(window).keyup(function(event){
						  if (event.keyCode == 13){
							  focus = true;
							  obj.load();// 查询按钮
						  }
					  });
					  $("#delete").click(function(){
						    var $check = $(".dq_dt_tab_chebox:checked");
						    if ($check.size()==0){
						    	alert("请选择需要删除的记录");
						    	return;
						    }
						    var text = $(this).text();
						    var url = $(this).attr("url");
						    Wconfirm("确定"+text+"选中的["+($check.size()-1)+"] 条记录",{
			                    type: "confirm",
			                    no: function(){
			                    },
			                    yes: function(){
			                    	 Wloading3("正在处理中请稍候");
						    		var data = "";
						    		  $check.each(function(index){
						    			   if (index>0){
						    				   data+="&ids="+$(this).val();
						    			   }
						    		  });
						    		  $.ajax({
						    			  type:"post",
						    			  url:url,
						    			  data:data,
						    			  success:function(data){
						    				  
						    				  if ("true" == data){
						    					   Wtip(text+"成功!",{ok:function(){
						    						   obj.load();
						    						   $("#dq_dt_tab_chebox_id").removeAttr("checked");
						    					   }} 
						    					   );
						    				   }else{
						    					   if (data.length < 100)
						    						   Wtip(data,{ok:function(){
							    						   obj.load();
							    						   $("#dq_dt_tab_chebox_id").removeAttr("checked");
							    					   }} 
							    					   );
						    					   else{
						    						   document.write(data);
						    					   }
						    				   }
						    			  },
						    			  error:function(){
						    				  
						    				  alert("连接服务器失败!");
						    			  }
						    		  });
			                    }
			                });
					  });
					  //编辑
					  $("#edit").click(function(){
						  
						    var $check = $(".dq_dt_tab_chebox:checked");
						    if ($check.size() != 2){
						    	alert("请选择一条记录编辑");
						    	return;
						    }
						    var url = $(this).attr("url");
						    var id = 0;
						    Wloading3("请稍候");
						    window.location= url+"&id="+$check.eq(1).val();
						    });
				  };
				  if (obj.dbclick){
					  $(".dq_dt_tab_tr").dblclick(function(){
						  var $check = $(this).find("input");
						    var url = $("#edit").attr("url");
						    var id = $check.val();
						    Wloading3("请稍候");
						    window.location= url+"&id="+id;
					  });
				  }
				  closeDialog();
		},
		error:function(data){
			closeDialog();
			alert("加载失败");
		}
	});
	index++;
};
function check_one(obj){
	var check = $(obj).parent().find("input");
	check.attr("checked",check.attr("checked")?false:true);
	if (check.is(":checked")){
		$(obj).parent("tr").find("td").css("backgroundColor","#fae2cd");
	}else{
		$(obj).parent("tr").find("td").css("backgroundColor","");
	}
	var ch = $(".dq_dt_tab_chebox:checked");
	if (ch.size() > 1){
		$("#dq_dt_tab_chebox_id").attr("checked",true);
	}
	else if (ch.size() == 1){
		if ($("#dq_dt_tab_chebox_id").attr("checked")){
			$("#dq_dt_tab_chebox_id").removeAttr("checked");
		}else{
			$("#dq_dt_tab_chebox_id").attr("checked",true);
		}
	}
	
}
function cheAllRow(curr){//选中所有checkbox
	if($(curr).is(":checked")){
		$("input[name='checkbox_dq']").attr("checked",true);
		$("input[name='checkbox_dq']").parent("td").parent("tr").find("td").css("backgroundColor","#fae2cd");
		}else{
			$("input[name='checkbox_dq']").removeAttr("checked");
			$("input[name='checkbox_dq']").parent("td").parent("tr").find("td").css("backgroundColor","");
			}
	
	}
//分页插件
/**
2014-08-05 ch
**/
(function($){
	var ms = {
		init:function(obj,args){
			return (function(){
				ms.fillHtml(obj,args);
				ms.bindEvent(obj,args);
			})();
		},
		//填充html
		fillHtml:function(obj,args){
			return (function(){
				//上一页
				obj.empty();
				if(args.current > 1){
				        obj.append('<a href="javascript:;" class="firstPage">首页</a>');
					obj.append('<a href="javascript:;" class="prevPage">上一页</a>');
				}else{
				        obj.append('<span class="disabled">首页</span>');
					obj.remove('.prevPage');
					obj.append('<span class="disabled">上一页</span>');
				}
				//中间页码
				if(args.current != 1 && args.current >= 4 && args.pageCount != 4){
					obj.append('<a href="javascript:;" class="tcdNumber">'+1+'</a>');
				}
				if(args.current-2 > 2 && args.current <= args.pageCount && args.pageCount > 5){
					obj.append('<span class="disabled">...</span>');
				}
				var start = args.current -2,end = args.current+2;
				if((start > 1 && args.current < 4)||args.current == 1){
					end++;
				}
				if(args.current > args.pageCount-4 && args.current >= args.pageCount){
					start--;
				}
				for (;start <= end; start++) {
					if(start <= args.pageCount && start >= 1){
						if(start != args.current){
							obj.append('<a href="javascript:;" class="tcdNumber">'+ start +'</a>');
						}else{
							obj.append('<span class="current">'+ start +'</span>');
						}
					}
				}
				if(args.current + 2 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 5){
					obj.append('<span class="disabled">...</span>');
				}
				if(args.current != args.pageCount && args.current < args.pageCount -2  && args.pageCount != 4){
					obj.append('<a href="javascript:;" class="tcdNumber">'+args.pageCount+'</a>');
				}
				//下一页
				if(args.current < args.pageCount){
					obj.append('<a href="javascript:;" class="nextPage">下一页</a>');
					obj.append('<a href="javascript:;" class="lastPage">末页</a>');
				}else{
					obj.remove('.nextPage');
					obj.append('<span class="disabled">下一页</span>');
					obj.append('<span class="disabled">末页</span>');
				}
			})();
		},
		//绑定事件
		bindEvent:function(obj,args){
			return (function(){
				obj.on("click","a.tcdNumber",function(){
					var current = parseInt($(this).text());
					ms.fillHtml(obj,{"current":current,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(current);
					}
				});
				//上一页
				obj.on("click","a.prevPage",function(){
					var current = parseInt(obj.children("span.current").text());
					ms.fillHtml(obj,{"current":current-1,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(current-1);
					}
				});
				//下一页
				obj.on("click","a.nextPage",function(){
					var current = parseInt(obj.children("span.current").text());
					ms.fillHtml(obj,{"current":current+1,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(current+1);
					}
				});
				obj.on("click","a.lastPage",function(){
					ms.fillHtml(obj,{"current":args.pageCount,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(args.pageCount);
					}
				});
				obj.on("click","a.firstPage",function(){
					ms.fillHtml(obj,{"current":1,"pageCount":args.pageCount});
					if(typeof(args.backFn)=="function"){
						args.backFn(1);
					}
				});
			})();
		}
	}
	$.fn.createPage = function(options){
		var args = $.extend({
			pageCount : 10,
			current : 1,
			backFn : function(){}
		},options);
		ms.init(this,args);
	}
})(jQuery);
$(document).ready(function(){ // 为input控件注入值

  $("input[type='text'][class!='Wdate']").each(function(){
	  var txt = $(this).attr("txt");
	  $(this).css("color","#444");
	  if (undefined != txt && txt != ""){
		  $(this).val(txt);
		  $(this).bind("focus",function(){
			  if($(this).val() == txt){
				  $(this).val("");
			  }
		  });
		  $(this).bind("blur",function(){
			  if ($(this).val() == ""){
				  $(this).val(txt);
			  }
		  });
	  }
  });	
});;
//